#include "logger.hpp"
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"

#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "123456"
#define DBNAME "gobang1"

void mysql_test()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASS, DBNAME, PORT);
    const char *sql = "insert into stu values(null,'小黑子',10,11,12,13);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (ret == false)
    {
        return;
    }
    mysql_util::mysql_destory(mysql);
    // fprintf(stdout,"%s - %d\n" , "gm",100);
    // ILOG("gm");
    // DLOG("gm1");
    // ELOG("gm2");
}

void json_test()
{
    Json::Value root;

    root["姓名"] = "小明";
    root["年纪"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(88.8);
    root["成绩"].append(77.7);
    std::string str;
    bool ret = json_util::serialize(root, str);
    if (ret == false)
    {
        DLOG("序列化失败");
    }

    DLOG("%s", str.c_str());

    Json::Value val;
    ret = json_util::unserialize(str, val);
    std::cout << "-------------" << std::endl;
    std::cout << val["姓名"].asString() << std::endl;
    std::cout << "-------------" << std::endl;
    std::cout << val["年纪"].asInt() << std::endl;
    std::cout << "-------------" << std::endl;
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << val["成绩"][i].asFloat() << std::endl;
    }
}

void str_test()
{
    std::string str = ",,123,,,,,,,,,357,,,,789";
    std::vector<std::string> array;
    string_util::split(str, ",", array);
    for (auto s : array)
    {
        DLOG("%s", s.c_str());
    }
}

void file_test()
{
    std::string str;
    file_util::read("./Makefile", str);
    std::cout << str << std::endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    user["username"] = "xiaoming";
    ut.insert(user);
    // user["password"] = "123466";
    // bool ret = ut.lose(1);
    //  std::string str;
    //  json_util::serialize(user,str);
    //  DLOG("%s",str.c_str());
    //  if(ret == false)
    //  {
    //      DLOG("登录失败");
    //  }
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_game_room(uid, conn);
    if (om.is_in_game_room(uid))
    {
        DLOG("IN GAME HALL");
    }
    else
    {
        DLOG("NOT IN GAME HALL");
    }
    om.exit_game_room(uid);
    if (om.is_in_game_room(uid))
    {
        DLOG("IN GAME HALL");
    }
    else
    {
        DLOG("NOT IN GAME HALL");
    }
}

int main()
{
    // user_table ut(HOST, USER, PASS, DBNAME, PORT);
    // online_manager om;

    // room_manager rm(&ut, &om);
    // room_ptr rp = rm.create_room(10, 20);

    // // room r(10,&ut,&om);
    // // online_test();
    // matcher mc(&rm, &ut, &om);

    gobang_server _server(HOST, USER, PASS, DBNAME, PORT);
    _server.start(8800);
    return 0;
}
